package org.jasig.cas.util;

import java.util.Map;

import org.apache.log4j.Logger;
import org.quartz.Trigger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

public final class AutowiringSchedulerFactoryBean extends SchedulerFactoryBean implements ApplicationContextAware, InitializingBean {
    private ApplicationContext applicationContext;
    private final Logger log = Logger.getLogger(getClass());

    public void afterPropertiesSet() throws Exception {
        Map<?, Trigger> triggers = applicationContext.getBeansOfType(Trigger.class);
        super.setTriggers((Trigger[]) triggers.values().toArray(new Trigger[triggers.size()]));
        if (log.isDebugEnabled()) {
            log.debug("Autowired the following triggers defined in application context: " + triggers.keySet().toString());
        }
        super.afterPropertiesSet();
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        super.setApplicationContext(applicationContext);
        this.applicationContext = applicationContext;
    }
}
